#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
#include <QMessageBox>
#include <QDesktopServices>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    QDesktopServices::setUrlHandler("http", this, "openUrl");
    QDesktopServices::setUrlHandler("file", this, "openUrl");

    QDesktopServices::openUrl(QUrl("http://blog.csdn.net/liang19890820"));
    QDesktopServices::openUrl(QUrl("file:///D:/Program Files/Youdao"));
    ui->setupUi(this);
}

bool MainWindow::openUrl(const QUrl &url)
{
    QString strScheme = url.scheme();
    if (QString::compare(strScheme, "http", Qt::CaseInsensitive) == 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("提示"), QStringLiteral("打开的是一个网址：%1").arg(url.url()));
        return true;
    }
    else if(QString::compare(strScheme, "file", Qt::CaseInsensitive) == 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("提示"), QStringLiteral("打开的是一个文件：%1").arg(url.url()));
        return true;
    }

    return false;
}

MainWindow::~MainWindow()
{
    delete ui;
}
